package com.julis.activity.setting.deviceweihu;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.scj.tc.CMD_Manager;
import com.android.scj.tc.Connection;
import com.android.scj.tc.bean.InitRequest;
import com.android.scj.tc.bean.InitResponse;
import com.baseutils.SpfUtils;
import com.github.czy1121.view.RoundButton;
import com.julis.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Setting_Power_Activity extends AppCompatActivity {
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.seekBar)
    SeekBar seekBar;
    @BindView(R.id.layout_ftp)
    RelativeLayout layoutFtp;
    @BindView(R.id.tv_value)
    TextView tvValue;
    @BindView(R.id.activity_seetting)
    LinearLayout activitySeetting;
    @BindView(R.id.setting)
    RoundButton setting;
    int value = 0;
    CMD_Manager cmdManager;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settingpower);
        cmdManager = CMD_Manager.getCMDManager(Setting_Power_Activity.this);
        ButterKnife.bind(this);
        int gonglv = (int) SpfUtils.get(this, "gonglv", 1);
        value = gonglv;
        tvValue.setText("当前发送功率：" + gonglv);
        seekBar.setProgress(gonglv - 1);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {

                int i = progress + 1;
                tvValue.setText("当前发送功率：" + i);
                value = i;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


    }

    @OnClick({R.id.img_back, R.id.setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:



                finish();
                break;
            case R.id.setting:
                //
                try {
                    setting();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    Connection[] con;

    public void setting() throws Exception {

         con = cmdManager.getConnection();

        String hex = Integer.toHexString(value);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        String cmdstr = InitRequest.create("1E", "14"  , "" + hex, "00" ).getCMDSTR();
        InitResponse ir = InitResponse.disassemble(con[0].send(cmdstr, Setting_Power_Activity.this));
        if (ir == null) {
            Toast.makeText(this, "发射功率设置失败！", Toast.LENGTH_SHORT).show();
            throw new Exception("功率信息初始化失败!");
        }
        if ("00".equals(ir.getRSUStatus())) {
            System.out.println("功率信息初始化成功!");
            SpfUtils.put(this, "gonglv", value);
            Toast.makeText(this, "发射功率设置成功！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "发射功率设置失败！", Toast.LENGTH_SHORT).show();
            throw new Exception("功率信息初始化失败!");
        }

    }

}
